package o;

import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.mkv.EbmlReaderOutput;
import java.io.IOException;
import java.util.ArrayDeque;

public final class aek implements aej {
    private int ʻ;
    private EbmlReaderOutput ˊ;
    private int ˋ;
    private final aes ˎ = new aes();
    private final byte[] ˏ = new byte[8];
    private final ArrayDeque<b> ॱ = new ArrayDeque();
    private long ॱॱ;

    static final class b {
        private final long ˏ;
        private final int ॱ;

        private b(int i, long j) {
            this.ॱ = i;
            this.ˏ = j;
        }
    }

    public void ˋ(EbmlReaderOutput ebmlReaderOutput) {
        this.ˊ = ebmlReaderOutput;
    }

    public void ˎ() {
        this.ˋ = 0;
        this.ॱ.clear();
        this.ˎ.ˊ();
    }

    public boolean ˋ(adv o_adv) throws IOException, InterruptedException {
        amm.ˏ(this.ˊ != null);
        while (true) {
            if (this.ॱ.isEmpty() || o_adv.ˊ() < ((b) this.ॱ.peek()).ˏ) {
                if (this.ˋ == 0) {
                    long ˊ = this.ˎ.ˊ(o_adv, true, false, 4);
                    if (ˊ == -2) {
                        ˊ = ॱ(o_adv);
                    }
                    if (ˊ == -1) {
                        return false;
                    }
                    this.ʻ = (int) ˊ;
                    this.ˋ = 1;
                }
                if (this.ˋ == 1) {
                    this.ॱॱ = this.ˎ.ˊ(o_adv, false, true, 8);
                    this.ˋ = 2;
                }
                int ॱ = this.ˊ.ॱ(this.ʻ);
                switch (ॱ) {
                    case 0:
                        o_adv.ॱ((int) this.ॱॱ);
                        this.ˋ = 0;
                    case 1:
                        long ˊ2 = o_adv.ˊ();
                        this.ॱ.push(new b(this.ʻ, this.ॱॱ + ˊ2));
                        this.ˊ.ॱ(this.ʻ, ˊ2, this.ॱॱ);
                        this.ˋ = 0;
                        return true;
                    case 2:
                        if (this.ॱॱ > 8) {
                            throw new ParserException("Invalid integer size: " + this.ॱॱ);
                        }
                        this.ˊ.ˋ(this.ʻ, ˎ(o_adv, (int) this.ॱॱ));
                        this.ˋ = 0;
                        return true;
                    case 3:
                        if (this.ॱॱ > 2147483647L) {
                            throw new ParserException("String element size: " + this.ॱॱ);
                        }
                        this.ˊ.ॱ(this.ʻ, ˊ(o_adv, (int) this.ॱॱ));
                        this.ˋ = 0;
                        return true;
                    case 4:
                        this.ˊ.ˏ(this.ʻ, (int) this.ॱॱ, o_adv);
                        this.ˋ = 0;
                        return true;
                    case 5:
                        if (this.ॱॱ == 4 || this.ॱॱ == 8) {
                            this.ˊ.ˎ(this.ʻ, ˏ(o_adv, (int) this.ॱॱ));
                            this.ˋ = 0;
                            return true;
                        }
                        throw new ParserException("Invalid float size: " + this.ॱॱ);
                    default:
                        throw new ParserException("Invalid element type " + ॱ);
                }
            }
            this.ˊ.ˊ(((b) this.ॱ.pop()).ॱ);
            return true;
        }
    }

    private long ॱ(adv o_adv) throws IOException, InterruptedException {
        o_adv.ˏ();
        while (true) {
            o_adv.ˎ(this.ˏ, 0, 4);
            int ˋ = aes.ˋ(this.ˏ[0]);
            if (ˋ != -1 && ˋ <= 4) {
                int ॱ = (int) aes.ॱ(this.ˏ, ˋ, false);
                if (this.ˊ.ˎ(ॱ)) {
                    o_adv.ॱ(ˋ);
                    return (long) ॱ;
                }
            }
            o_adv.ॱ(1);
        }
    }

    private long ˎ(adv o_adv, int i) throws IOException, InterruptedException {
        int i2 = 0;
        o_adv.ˏ(this.ˏ, 0, i);
        long j = 0;
        while (i2 < i) {
            j = (j << 8) | ((long) (this.ˏ[i2] & 255));
            i2++;
        }
        return j;
    }

    private double ˏ(adv o_adv, int i) throws IOException, InterruptedException {
        long ˎ = ˎ(o_adv, i);
        if (i == 4) {
            return (double) Float.intBitsToFloat((int) ˎ);
        }
        return Double.longBitsToDouble(ˎ);
    }

    private String ˊ(adv o_adv, int i) throws IOException, InterruptedException {
        if (i == 0) {
            return "";
        }
        byte[] bArr = new byte[i];
        o_adv.ˏ(bArr, 0, i);
        while (i > 0 && bArr[i - 1] == (byte) 0) {
            i--;
        }
        return new String(bArr, 0, i);
    }
}
